import { asyncRoutes, constantRoutes } from '@/router'
/**
 * 比较'name'  to determine if the current user has permission
 * @param authedRoutes
 * @param route
 */
function hasPermission(authedRoute, route) {
  if (route.name) {
    // 对一些特的菜单进行处理，如部门，可让全体查看，但按权限修改
    if (route.name === 'Department') {
      return true
    }
    // 添加隐藏页面，显示链接由菜单页面按权限控制
    if (route.hidden) {
      return true
    }
    return route.name === authedRoute.name
  } else {
    return false
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param authedRoutes
 */
export function filterAsyncRoutes(routes, authedRoutes) {
  const res = []
  if (authedRoutes && authedRoutes.length > 0) {
    routes.forEach(route => {
      const tmp = { ...route }
      for (let j = 0, len = authedRoutes.length; j < len; j++) {
        if (hasPermission(authedRoutes[j], tmp)) {
          if (tmp.children) {
            tmp.children = filterAsyncRoutes(tmp.children, authedRoutes[j].children)
          }
          res.push(tmp)
          break
        }
      }
    })
  }
  return res
}

const state = {
  routes: [],
  addRoutes: [],
  modules: [],
  curModule: { name: '', title: '', icon: '', path: '' },
  curModuleRoutes: [],
  rederictModle: ''
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes

    // default module
    const defaultModule = state.rederictModle === '' ? 'Edu' : state.rederictModle
    let cur = null
    const modules = []
    for (const r in routes) {
      if (routes[r].children && routes[r].children.length > 0) {
        modules.push(routes[r])
        if (routes[r].name === defaultModule) {
          cur = routes[r]
        }
      }
    }
    state.modules = modules
    if (cur) {
      state.curModuleRoutes = cur.children
      state.curModule = { name: cur.name, path: cur.path, title: cur.meta.title, icon: cur.meta.icon }
    } else if (modules.length > 0) {
      state.curModuleRoutes = modules[0].children
      state.curModule = { name: modules[0].name, path: modules[0].path, title: modules[0].meta.title, icon: modules[0].meta.icon }
    }

    // all routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_CURMODULE: (state, module) => {
    const routes = state.addRoutes
    let cur = null
    for (const r in routes) {
      if (routes[r].children && routes[r].children.length > 0) {
        if (routes[r].name === module) {
          cur = routes[r]
          break
        }
      }
    }
    if (cur) {
      state.curModuleRoutes = cur.children
      state.curModule = { name: cur.name, path: cur.path, title: cur.meta.title, icon: cur.meta.icon }
    }
  },
  SET_REDERICTMODULE: (state, module) => {
    state.rederictModle = module
  }
}

const actions = {
  generateRoutes({ commit }, authedRoutes) {
    return new Promise(resolve => {
      const accessedRoutes = filterAsyncRoutes(asyncRoutes, authedRoutes)
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  },
  setCurModule({ commit }, module) {
    return new Promise(resolve => {
      commit('SET_CURMODULE', module)
      resolve()
    })
  },
  setRederictModule({ commit }, module) {
    return new Promise(resolve => {
      commit('SET_REDERICTMODULE', module)
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
